package logic

import (
	"context"
	"product_zc/models/mysql"

	"product_zc/productRpc/internal/svc"
	"product_zc/productRpc/productRpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ShowProductLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewShowProductLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ShowProductLogic {
	return &ShowProductLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ShowProductLogic) ShowProduct(in *productRpc.ShowProductRequest) (*productRpc.ShowProductResponse, error) {
	// todo: add your logic here and delete this line

	var product mysql.Product
	mysql.DB.Debug().Where("id = ?", in.Id).Find(&product)

	product.BrowseNumber = product.BrowseNumber + 1

	mysql.DB.Debug().Updates(&product)

	return &productRpc.ShowProductResponse{
		Id:           int64(product.ID),
		Title:        product.Title,
		Price:        float32(product.Price),
		BrowseNumber: product.BrowseNumber,
		UserId:       product.UserId,
	}, nil
}
